package interfaces;

/**
 * Clase InterfazLogin.
 * 
 * @author David González Requejo.
 */

public class InterfazLogin extends javax.swing.JFrame {

    /**
     * Crea un objeto de la clase InterfazLogin.
     */
    
    public InterfazLogin() {
        
        initComponents();
        this.setLocationRelativeTo( this.getParent() );
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        contenedorLoginJPanel = new javax.swing.JPanel();
        contenedorDatosAccesoJPanel = new javax.swing.JPanel();
        aliasJTextField = new javax.swing.JTextField();
        claveJPasswordField = new javax.swing.JPasswordField();
        nieJLabel = new javax.swing.JLabel();
        claveJLabel = new javax.swing.JLabel();
        accederJButton = new javax.swing.JButton();
        loginJLabel = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setResizable(false);

        contenedorLoginJPanel.setMaximumSize(new java.awt.Dimension(400, 240));
        contenedorLoginJPanel.setMinimumSize(new java.awt.Dimension(400, 240));

        contenedorDatosAccesoJPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)), "Datos de acceso"));

        nieJLabel.setText("Alias:");

        claveJLabel.setText("Clave:");

        javax.swing.GroupLayout contenedorDatosAccesoJPanelLayout = new javax.swing.GroupLayout(contenedorDatosAccesoJPanel);
        contenedorDatosAccesoJPanel.setLayout(contenedorDatosAccesoJPanelLayout);
        contenedorDatosAccesoJPanelLayout.setHorizontalGroup(
            contenedorDatosAccesoJPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(contenedorDatosAccesoJPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(contenedorDatosAccesoJPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(claveJLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(nieJLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 80, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(contenedorDatosAccesoJPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(aliasJTextField)
                    .addComponent(claveJPasswordField))
                .addContainerGap())
        );
        contenedorDatosAccesoJPanelLayout.setVerticalGroup(
            contenedorDatosAccesoJPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, contenedorDatosAccesoJPanelLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(contenedorDatosAccesoJPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(aliasJTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(nieJLabel))
                .addGap(16, 16, 16)
                .addGroup(contenedorDatosAccesoJPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(claveJPasswordField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(claveJLabel))
                .addContainerGap())
        );

        accederJButton.setText("Acceder");
        accederJButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                accederJButtonActionPerformed(evt);
            }
        });

        loginJLabel.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
        loginJLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        loginJLabel.setText("LOGIN");

        javax.swing.GroupLayout contenedorLoginJPanelLayout = new javax.swing.GroupLayout(contenedorLoginJPanel);
        contenedorLoginJPanel.setLayout(contenedorLoginJPanelLayout);
        contenedorLoginJPanelLayout.setHorizontalGroup(
            contenedorLoginJPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(contenedorLoginJPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(contenedorLoginJPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(contenedorLoginJPanelLayout.createSequentialGroup()
                        .addComponent(loginJLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 372, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 8, Short.MAX_VALUE))
                    .addGroup(contenedorLoginJPanelLayout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(accederJButton))
                    .addComponent(contenedorDatosAccesoJPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        contenedorLoginJPanelLayout.setVerticalGroup(
            contenedorLoginJPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, contenedorLoginJPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(loginJLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 74, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(contenedorDatosAccesoJPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(accederJButton)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(contenedorLoginJPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(contenedorLoginJPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * Método que se lanza al clicar en el botón acceder para validar los datos del formulario
     * del usuario que trata de autenticarse desde la interfaz.
     * 
     * @param evt 
     */
    
    private void accederJButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_accederJButtonActionPerformed
        
        utilidades.Login autenticarUsuario = new utilidades.Login();
        
        if( autenticarUsuario.validarUsuario( aliasJTextField.getText(), utilidades.Cifrados.cifrarMD5( new String( claveJPasswordField.getPassword() ) ) ) ) {
            
            switch( autenticarUsuario.getRol_usuario() ) {
                
                // Interfaz del conserje.
                case 2:
                    
                    InterfazDatos interfazConserje = new InterfazDatos( autenticarUsuario.getId_usuario() );
                    interfazConserje.setLocationRelativeTo( this.getParent() );
                    interfazConserje.setVisible( true );
                    this.dispose(); 
                    
                break;
                
                // Interfaz del tutor.
                case 3:
                    
                    InterfazTutor interfazTutor = new InterfazTutor( autenticarUsuario.getId_usuario() );
                    interfazTutor.setLocationRelativeTo( this.getParent() );
                    interfazTutor.setVisible( true );
                    this.dispose(); 
                
                break;
                    
                
            }
            
        }
        
    }//GEN-LAST:event_accederJButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton accederJButton;
    private javax.swing.JTextField aliasJTextField;
    private javax.swing.JLabel claveJLabel;
    private javax.swing.JPasswordField claveJPasswordField;
    private javax.swing.JPanel contenedorDatosAccesoJPanel;
    private javax.swing.JPanel contenedorLoginJPanel;
    private javax.swing.JLabel loginJLabel;
    private javax.swing.JLabel nieJLabel;
    // End of variables declaration//GEN-END:variables
}
